﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PDX;
using FluentValidation;
using FluentValidation.Results;
using System.ComponentModel;

namespace PdxDemo.FluentValidationDemo
{
    public class FluentValidationResult : INPC
    {
        INotifyPropertyChanged owner;
        IValidator validator;

        public FluentValidationResult(INotifyPropertyChanged owner, IValidator validator)
        {
            this.owner = owner as INotifyPropertyChanged;
            this.validator = validator;

            owner.PropertyChanged += new PropertyChangedEventHandler(owner_PropertyChanged);

            Revalidate();
        }

        void owner_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Revalidate();
        }

        void Revalidate()
        {
            Result = validator.Validate(owner);
        }

        ValidationResult _result;
        public ValidationResult Result
        {
            get
            {
                return _result;
            }
            private set
            {
                _result = value;
                OnPropertyChanged("Result");
            }
        }
    }
}
